'use strict'
// 用来配置文件的别名，方便路径的寻找
const path = require('path'); //引入path模块
const defaultSettings = require('./src/settings.js')
function resolve(dir) {
    return path.join(__dirname, dir)//path.join(__dirname)设置绝对路径
}
const port = process.env.port || process.env.npm_config_port || 9527 // dev port
const name = defaultSettings.title || '南海城市大脑_渔业资源管理系统、种植资源管理系统' // page title
const serverURL = [
    "http://192.168.104.240:8280/",
    "http://192.168.105.96:8280/",
    "http://218.75.111.145:8280/",
    'http://10.171.142.66:8282/'
]
module.exports = {
    publicPath: '/',
    outputDir: 'dist',
    assetsDir: 'static',
    lintOnSave: process.env.NODE_ENV === 'development',
    productionSourceMap: false,
    devServer: {
        port: port,
        open: true,
        overlay: {
            warnings: false,
            errors: true
        },
        proxy: {
            '/': {
                target: serverURL[2], // 请求的本域名
                secure: false,  // 如果是https接口，需要配置这个参数
                changeOrigin: true,  //是否跨域
            },
        },
    },
    configureWebpack: {
        name: name,
        resolve: {
            alias: {
                '@': resolve('src')
            }
        }
    },
    chainWebpack(config) {
        config.plugin('preload').tap(() => [
            {
                rel: 'preload',
                fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
                include: 'initial'
            }
        ])

        config.plugins.delete('prefetch')
        // config.module
        //     .rule('svg')
        //     .exclude.add(resolve('src/icons'))
        //     .end()
        // config.module
        //     .rule('icons')
        //     .test(/\.svg$/)
        //     .include.add(resolve('src/icons'))
        //     .end()
        //     .use('svg-sprite-loader')
        //     .loader('svg-sprite-loader')
        //     .options({
        //         symbolId: 'icon-[name]'
        //     })
        //     .end()

        config
            .when(process.env.NODE_ENV !== 'development',
                config => {
                    config
                        .plugin('ScriptExtHtmlWebpackPlugin')
                        .after('html')
                        .use('script-ext-html-webpack-plugin', [{
                            inline: /runtime\..*\.js$/
                        }])
                        .end()
                    config
                        .optimization.splitChunks({
                            chunks: 'all',
                            cacheGroups: {
                                libs: {
                                    name: 'chunk-libs',
                                    test: /[\\/]node_modules[\\/]/,
                                    priority: 10,
                                    chunks: 'initial'
                                },
                                elementUI: {
                                    name: 'chunk-elementUI',
                                    priority: 20,
                                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/
                                },
                                mapbox: {
                                    name: 'chunk-mapbox',
                                    priority: 20,
                                    test: /[\\/]node_modules[\\/]_?@mapbox(.*)/
                                },
                                supermap: {
                                    name: 'chunk-supermap',
                                    priority: 20,
                                    test: /[\\/]node_modules[\\/]_?@supermap(.*)/
                                },
                                commons: {
                                    name: 'chunk-commons',
                                    test: resolve('src/components'),
                                    minChunks: 3,
                                    priority: 5,
                                    reuseExistingChunk: true
                                },
                                store: {
                                    name: 'chunk-store',
                                    test: resolve('src/store'),
                                    minChunks: 3,
                                    priority: 5,
                                    reuseExistingChunk: true
                                },
                                api: {
                                    name: 'chunk-api',
                                    test: resolve('src/api'),
                                    minChunks: 3,
                                    priority: 5,
                                    reuseExistingChunk: true
                                }
                            }
                        })
                    config.optimization.runtimeChunk('single')
                }
            )
    }
}
